package com.example.hospital.patient.wx.api.service.impl;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.example.hospital.patient.wx.api.db.dao.UserInfoCardDao;
import com.example.hospital.patient.wx.api.db.pojo.UserInfoCardEntity;
import com.example.hospital.patient.wx.api.service.UserInfoCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

@Service
@Slf4j
public class UserInfoCardServiceImpl implements UserInfoCardService {

    @Resource
    private UserInfoCardDao userInfoCardDao;
    @Override
    public int insert(UserInfoCardEntity entity) {
        int insert = userInfoCardDao.insert(entity);
        log.info("###### insert userInfoCardInfo:{}", insert);
        return insert;
    }

    @Override
    public HashMap selectUserInfoCard(int userId) {
        HashMap map = userInfoCardDao.selectUserInfoCard(userId);
        if (map != null) {
            String medicalHistory = MapUtil.getStr(map, "medicalHistory");
            JSONArray array = JSONUtil.parseArray(medicalHistory);
            map.replace("medicalHistory", array);
        }
        return map;
    }

    @Override
    public int update(UserInfoCardEntity entity) {
        int update = userInfoCardDao.update(entity);
        log.info("###### update userInfoCardInfo:{}", update);
        return update;
    }
}
